/**
 * 视频播放器
 * jch
 */
!Ext.isChrome ? 
Ext.define('Sh.ux.VideoPlayer', {
	extend : 'Ext.panel.Panel',
	alias: 'widget.player',
	layout: 'border',
	bodyStyle: {background: 'black',color: 'white'},
	requires: ['Module.wg.WgEditWindow'],
	config: {
		src: '',
		vdata: {},
		autoPlay: false,
//		isplay: false,
		autoLoadVideo: false,
		loaded: false, 
		oldtime: 0,
		onloadeddata: function(){},
		hides: [/*'play','progress','time','full','sign'*/]
	},
	loadedVideo: function(){
		var me = this;
		this.setLoaded(true);
		if(me.onloadeddata){
			me.onloadeddata();
		}
		
	},
	initComponent : function(config) {
		var me = this;
		me.on('boxready',function(p){
			me.initVideo();
			me.initMenu();
			if(me.hides && me.hides.length > 0){
				for ( var i = 0; i < me.hides.length; i++) {
					Ext.getCmp(me.id+'-'+me.hides[i]).setVisible(false);
				}
			}
			preventDefaultMenu(me.body.dom);
			
		});
		
		me.on('beforedestroy',function(){
			try {
				if(me.task)me.task.stop();
				$('#' + me.id + '-player').remove();
			} catch (e) {
				// TODO: handle exception
			}
		});

		me.bbar = [{
			id:me.id+'-play',
			iconCls: me.autoPlay ?'icon-v_pause':'icon-v_play',
			width:23,
			handler: function(){
				if(!me.isPlay()){ 
					me.play();
				}
				else{
					me.pause();
				}
			}
		},{
			xtype:'progressbar',
			id: me.id+'-progress',
			width:150,
			style: {cursor: 'pointer','font-size': '12px'},
			listeners:{
				afterrender: function(prbar){
					var runner = new Ext.util.TaskRunner();
					me.task = runner.newTask({
						run: function () {
							try {
								var nowtime = me.getCurrentTime();
								var alltime = me.getAllTime();
								var ts = '00:00/00:00';
								if(alltime != undefined && nowtime != undefined){
									if(!me.loaded){
										me.loaded == true;
										me.loadedVideo();
									}
									ts = timeFormat(nowtime)+'/'+timeFormat(alltime);
									prbar.updateProgress((nowtime/alltime),'');
									if(nowtime == alltime){
										Ext.getCmp(me.id+'-play').setIconCls('icon-v_play');
										task.stop();
									}
								}
								Ext.getCmp(me.id+'-time').update({
									html: '<span style="font-size:12px;">'+ts+'</span>'
								});
								
							} catch (e) {}
						},
						interval: 500
					}).start();

					//视频进度点击事件
					prbar.el.on('click',function(e,t){
						var el = prbar.el; 
						var x = e.getX();
						var _t = (x-el.getLeft())*(me.getAllTime()/prbar.width);
						me.goToTime(_t);
					});

					prbar.addManagedListener(me,'resize', function(_me,w,h){
						if(w > 0 && h > 0){
							var prbar = me.down('progressbar');
//							$('video',me.el.dom).width(me.body.getWidth()-2.5).height(me.body.getHeight()-5);
							me.setVideoSize(me.body.getWidth()-2.5, me.body.getHeight()-5);
							var items = prbar.ownerCt.items.items;
							var ow = 5;
							for ( var i = 0; i < items.length; i++) {
								if(prbar != items[i] && items[i].isVisible() && items[i].xtype != 'tbfill'){
									ow += items[i].getWidth()+3;
								}
							}

							prbar.setWidth(w - ow);
						}
					});
				}
			}

		},'->',{
			width:80,
			html:'<span style="font-size:12px;">00:00/00:00</span>',
			id: me.id+'-time',
			disabled:true
		},{
			width:23,
			id:me.id+'-full',
			iconCls: 'icon-v_full_0',
			handler: function(){
				me.fullVideo();
			}
		},{
			width:23,
			id:me.id+'-sign',
			iconCls: 'icon-v_md',
			handler: function(){
				Ext.getCmp(me.id+'-play').setIconCls('icon-v_play');
				me.pause();
				me.signVideo();
			}
		}];

		me.callParent();
	},
	initVideo: function(){
		var me = this;
		var id = this.body.dom.id;
		var vwidth = me.body.getWidth()-2.5;
		var vheight = me.body.getHeight()-5;
		var flashvars={
				f: me.src,
				a:'',
				b:0,
				s:0,
				c:0,
				x:'',
				u:'',
				e:2,
				v:'80',
				h:'0',
				q:'',
				i: (!me.autoLoadVideo && me.vdata) ? ('videomanager/getVideoPic.do?videoid='+me.vdata.id+'&time=0') : '',
				g: me.oldtime ? me.oldtime : 0,
				p: me.autoPlay ? '1' : (me.autoLoadVideo ? '0' : '2')/*,
				loaded: "function(){Ext.getCmp('"+me.id+"').loadedVideo();}"*/
		};
		var params= {bgcolor:'#FFF',allowFullScreen:true,allowScriptAccess:'always',wmode:'transparent',menu: false};
		var a = CKobject.embedSWF('javascript/ckplayer/ckplayer.swf',id,me.id+'-player',vwidth,vheight,flashvars,params);
		/*var video=[];
		var support=['iPad','iPhone','ios','android+false','msie10+false'];
		var b = CKobject.embedHTML5(id,id+'-player',vwidth,vheight,video,flashvars,support);*/
	},
	initMenu: function(){
		var me = this;
		me.contextmenu = Ext.create('Ext.menu.Menu',{
			width: 100,
			minWidth: 100,
			items: [{
				text: '播放',
				iconCls: 'icon-v_play',
				handler: function(){
					if(!me.isPlay()){ 
						me.play();
					}
				}
			},/*{
				text: '暂停',
				iconCls: 'icon-v_pause',
				handler: function(){
					me.pause();
				}
			},*/{
				text: '停止',
				iconCls: 'icon-v_stop',
				handler: function(){
					me.pause();
					me.goToTime(0);
				}
			}]
		});
		me.body.on('contextmenu',function(e,t){
			me.contextmenu.showAt(e.getXY());
		});
		me.body.on('mouseup',function(e){
			if(e.button == 0)
			me.playOrPause();
		});
	},
	removeVideo: function(){
		var me = this;
		this.loaded = false;
		try {
			this.setOldtime(me.getCurrentTime());
			this.getPlayer().videoClear();
			$('*',this.body.dom).remove();
		} catch (e) {
			// TODO: handle exception
		}
	},
	isPlay: function(){
		var pl = this.getPlayer();
		return pl ? pl.getStatus().play : false;
	},
	playOrPause: function(){
		var me = this;
		try {
			this.getPlayer().playOrPause(); 
			me.isPlay()?Ext.getCmp(me.id+'-play').setIconCls('icon-v_pause')
					:Ext.getCmp(me.id+'-play').setIconCls('icon-v_play');
		} catch (e) {
			// TODO: handle exception
		}
	},
	getPlayer: function(){
		return CKobject.getObjectById(this.id+'-player');
		
	},
	play: function(){
		var me = this;
		try {
			this.getPlayer().videoPlay();
			Ext.getCmp(me.id+'-play').setIconCls('icon-v_pause');
		} catch (e) {
		}
		
	},
	pause: function(){
		var me = this;
		try {
			this.getPlayer().videoPause();
			Ext.getCmp(me.id+'-play').setIconCls('icon-v_play');
		} catch (e) {
		}
	},
	getAllTime: function(){
		var pl = this.getPlayer();
		
		return pl ? this.getPlayer().getStatus().totalTime : 0;
	},
	goToTime: function(t){
		try {
			var at = this.getAllTime();
			t = t > at ? at : t;
			this.getPlayer().videoSeek(t);
		} catch (e) {
		}
	},
	getCurrentTime: function(){
		var pl = this.getPlayer();
		return pl ? pl.getStatus().time : 0;
	},
	videoClear: function(){
		this.getPlayer().videoClear();
	},
	setVideoSize: function(width,height){
		var pl = this.getPlayer();
		if(pl){
			pl.width = width;
			pl.height = height;
		}
	},
	signVideo: function(){
		var me = this;
			Ext.create('Module.wg.WgEditWindow',{
				p: me,
				vdata: me.vdata,
				vsrc: me.src,
				player: me,
				maximized: true,
				closeAction: 'destroy'
			}).show();
	},
	fullVideo:function(isplay){
		var me = this;
		Ext.create('Ext.window.Window',{
			width: 400,
			height: 300,
			maximized: true,
			modal: true,
			layout: 'fit',
			items: [{
				xtype: 'player',
				src: me.src,
				vdata: me.vdata,
				autoPlay: me.isPlay(),
				autoLoadVideo: true,
				oldtime: me.getCurrentTime(),
				hides: me.hides,
				onloadeddata: function(){
					
				}
			}],
			listeners: {
				afterrender: function(w){
					var p = w.down('player');
					me.pause();
					Ext.getCmp(p.id+'-full').setIconCls('icon-v_full_1');
					p.fullVideo = function(){
						p.up('window').close();
					};
				},
				close: function(w){
					var p = w.down('player');
					me.goToTime(p.getCurrentTime());
					if(p.isPlay()){
						me.play();
					}
				}
			}
		}).show();
	},
	getFullRaw:function(){
		var me = this;
		var ownerct = me.ownerCt;
		var owidth = me.getWidth();
		var oheight = me.getHeight();
		var v = me.getPlayer();
		var oitems = ownerct.items.items;
		var index = 0;
		for ( var i = 0; i < oitems.length; i++) {
			if(oitems[i] == me){
				index = i;
				break;
			}
		}
		return {index:index,owidth:v.width,oheight:v.height,v:v,x:me.x,ownerCt: ownerct,otime: me.getCurrentTime(),osize: me.getSize()};
	}
}):Ext.define('Sh.ux.VideoPlayer', {
	extend : 'Ext.panel.Panel',
	alias: 'widget.player',
	layout: 'border',
	bodyStyle: {background: 'black',color: 'white'},
	requires: ['Module.wg.WgEditWindow'],
	config: {
		src: '',
		vdata: {},
		autoPlay: false,
//		isplay: false,
		autoLoadVideo: false,
		loaded: false, 
		oldtime: 0,
		onloadeddata: function(){},
		hides: [/*'play','progress','time','full','sign'*/]
	},
	loadedVideo: function(){
		var me = this;
		this.setLoaded(true);
		if(me.onloadeddata){
			me.onloadeddata();
		}
		
	},
	initComponent : function(config) {
		var me = this;
		me.on('boxready',function(p){
			me.initVideo();
			me.initMenu();
			if(me.hides && me.hides.length > 0){
				for ( var i = 0; i < me.hides.length; i++) {
					Ext.getCmp(me.id+'-'+me.hides[i]).setVisible(false);
				}
			}
			preventDefaultMenu(me.body.dom);
			
		});
		
		me.on('beforedestroy',function(){
			try {
				if(me.task)me.task.stop();
				$('#' + me.id + '-player').remove();
			} catch (e) {
				// TODO: handle exception
			}
		});

		me.bbar = [{
			id:me.id+'-play',
			iconCls: me.autoPlay ?'icon-v_pause':'icon-v_play',
			width:23,
			handler: function(){
				if(!me.isPlay()){ 
					me.play();
				}
				else{
					me.pause();
				}
			}
		},{
			xtype:'progressbar',
			id: me.id+'-progress',
			width:150,
			style: {cursor: 'pointer','font-size': '12px'},
			listeners:{
				afterrender: function(prbar){
					var runner = new Ext.util.TaskRunner();
					me.task = runner.newTask({
						run: function () {
							try {
								var nowtime = me.getCurrentTime();
								var alltime = me.getAllTime();
								var ts = '00:00/00:00';
								if(alltime != undefined && nowtime != undefined){
									if(!me.loaded){
										me.loaded == true;
										me.loadedVideo();
									}
									ts = timeFormat(nowtime)+'/'+timeFormat(alltime);
									prbar.updateProgress((nowtime/alltime),'');
									if(nowtime == alltime){
										Ext.getCmp(me.id+'-play').setIconCls('icon-v_play');
										task.stop();
									}
								}
								Ext.getCmp(me.id+'-time').update({
									html: '<span style="font-size:12px;">'+ts+'</span>'
								});
								
							} catch (e) {}
						},
						interval: 500
					}).start();

					//视频进度点击事件
					prbar.el.on('click',function(e,t){
						var el = prbar.el; 
						var x = e.getX();
						var _t = (x-el.getLeft())*(me.getAllTime()/prbar.width);
						me.goToTime(_t);
					});

					prbar.addManagedListener(me,'resize', function(_me,w,h){
						if(w > 0 && h > 0){
							var prbar = me.down('progressbar');
//							$('video',me.el.dom).width(me.body.getWidth()-2.5).height(me.body.getHeight()-5);
							me.setVideoSize(me.body.getWidth()-2.5, me.body.getHeight()-5);
							var items = prbar.ownerCt.items.items;
							var ow = 5;
							for ( var i = 0; i < items.length; i++) {
								if(prbar != items[i] && items[i].isVisible() && items[i].xtype != 'tbfill'){
									ow += items[i].getWidth()+3;
								}
							}

							prbar.setWidth(w - ow);
						}
					});
				}
			}

		},'->',{
			width:80,
			html:'<span style="font-size:12px;">00:00/00:00</span>',
			id: me.id+'-time',
			disabled:true
		},{
			width:23,
			id:me.id+'-full',
			iconCls: 'icon-v_full_0',
			handler: function(){
				me.fullVideo();
			}
		},{
			width:23,
			id:me.id+'-sign',
			iconCls: 'icon-v_md',
			handler: function(){
				Ext.getCmp(me.id+'-play').setIconCls('icon-v_play');
				me.pause();
				me.signVideo();
			}
		}];

		me.callParent();
	},
	initVideo: function(){
		var me = this;
		var id = this.body.dom.id;
		var vwidth = me.body.getWidth()-2.5;
		var vheight = me.body.getHeight()-5;
		me.update({
			html: '<video onloadeddata="Ext.getCmp(\''+me.id+'\').onloadeddata();" src="'+me.src
			+'" '+(me.autoPlay ? 'autoplay' : '')+'>您的浏览器太落后了，不支持 video 标签。</video>'
		});
		
		if(me.oldtime){
			me.goToTime(me.oldtime);
		}
	},
	initMenu: function(){
		var me = this;
		me.contextmenu = Ext.create('Ext.menu.Menu',{
			width: 100,
			minWidth: 100,
			items: [{
				text: '播放',
				iconCls: 'icon-v_play',
				handler: function(){
					if(!me.isPlay()){ 
						me.play();
					}
				}
			},/*{
				text: '暂停',
				iconCls: 'icon-v_pause',
				handler: function(){
					me.pause();
				}
			},*/{
				text: '停止',
				iconCls: 'icon-v_stop',
				handler: function(){
					me.goToTime(0);
					me.pause();
					me.goToTime(0);
				}
			}]
		});
		me.body.on('contextmenu',function(e,t){
//			alert(Ext.encode(e.getXY()));
			var xy = e.getXY();
			me.contextmenu.showAt([xy[0]+5,xy[1]]);
		});
		me.body.on('mouseup',function(e){
			if(e.button == 0)
			me.playOrPause();
		});
	},
	removeVideo: function(){
		var me = this;
		this.loaded = false;
		try {
			this.setOldtime(me.getCurrentTime());
			$('*',this.body.dom).remove();
		} catch (e) {
			// TODO: handle exception
		}
	},
	isPlay: function(){
		var me = this;
		return !me.getPlayer().paused;
	},
	playOrPause: function(){
		var me = this;
		try {
			var isplay = this.isPlay();
			if(isplay){
				me.play();
			}
			else{
				me.pause();
			}
		} catch (e) {
			// TODO: handle exception
		}
	},
	getPlayer: function(){
		var me = this;
		return $("video",me.el.dom).get(0);
	},
	play: function(){
		try {
			var me = this;
			$('video',me.el.dom).trigger('play');
			Ext.getCmp(me.id+'-play').setIconCls('icon-v_pause');
		} catch (e) {
		}
		
	},
	pause: function(){
		try {
			var me = this;
			$('video',me.el.dom).trigger('pause');
			Ext.getCmp(me.id+'-play').setIconCls('icon-v_play');
		} catch (e) {
		}
	},
	getAllTime: function(){
		var me = this;
		return me.getPlayer().duration;
	},
	goToTime: function(t){
		var me = this;
		me.getPlayer().currentTime = t;
	},
	getCurrentTime: function(){
		var me = this;
		
		return me.getPlayer().currentTime;
	},
	videoClear: function(){
		$('video',this.el.dom).remove();
	},
	setVideoSize: function(width,height){
		$('video',this.el.dom).width(width).height(height);
	},
	signVideo: function(){
		var me = this;
			Ext.create('Module.wg.WgEditWindow',{
				p: me,
				vdata: me.vdata,
				vsrc: me.src,
				player: me,
				maximized: true,
				closeAction: 'destroy'
			}).show();
	},
	fullVideo:function(isplay){
		var me = this;
		Ext.create('Ext.window.Window',{
			width: 400,
			height: 300,
			maximized: true,
			modal: true,
			layout: 'fit',
			items: [{
				xtype: 'player',
				src: me.src,
				vdata: me.vdata,
				autoPlay: me.isPlay(),
				autoLoadVideo: true,
				oldtime: me.getCurrentTime(),
				hides: me.hides,
				onloadeddata: function(){
					
				}
			}],
			listeners: {
				afterrender: function(w){
					var p = w.down('player');
					me.pause();
					Ext.getCmp(p.id+'-full').setIconCls('icon-v_full_1');
					p.fullVideo = function(){
						p.up('window').close();
					};
				},
				close: function(w){
					var p = w.down('player');
					me.goToTime(p.getCurrentTime());
					if(p.isPlay()){
						me.play();
					}
				}
			}
		}).show();
	}
});

